// var n;
// var r = cc._decorator;
// var c = r.ccclass;
// var l =
//     (r.property,
//     (function (e) {
//         function t() {
//             return (null !== e && e.apply(this, arguments)) || this;
//         }
//         __extends(t, e);
//         t.prototype.show = function (e) {
//             var t = this;
//             var o = e.gameName;
//             var i = e.gameIcon;
//             var n = e.rewardIcon;
//             var a = e.onReward;
//             var s = cc.find("window", this.node);
//             var r = cc.find("nameLabel1", s).getComponent(cc.Label);
//             var c = cc.find("nameLabel2", s).getComponent(cc.Label);
//             var l = cc.find("get_btn", s);
//             var d = cc.find("go_btn", s);
//             var u = cc.find("icon", s).getComponent(cc.Sprite);
//             var h = cc.find("rewardIcon", s).getComponent(cc.Sprite);
//             s.scale = Math.min(cc.winSize.width / cc.view.getDesignResolutionSize().width, 1);
//             r.string = o;
//             o.length > 4 ? (c.string = o.slice(0, 4) + "...") : (c.string = o);
//             l.active = e.xianshi_jiangli;
//             d.active = !e.xianshi_jiangli;
//             l.on(
//                 cc.Node.EventType.TOUCH_END,
//                 function () {
//                     console.log("touch get_btn");
//                     if (a) {
//                         a();
//                     }
//                     t.hide();
//                 },
//                 this
//             );
//             d.on(
//                 cc.Node.EventType.TOUCH_END,
//                 function () {
//                     console.log("touch go_btn");
//                     tt.navigateToScene({
//                         scene: "sidebar",
//                         success: function () {
//                             console.log("sidebar跳转成功");
//                             t.hide();
//                         },
//                         fail: function () {
//                             console.log("sidebar跳转失败");
//                         }
//                     });
//                 },
//                 this
//             );
//             if (i) {
//                 u.spriteFrame = i;
//             }
//             if (n) {
//                 h.spriteFrame = n;
//             }
//             this.node.on(
//                 cc.Node.EventType.TOUCH_END,
//                 function () {
//                     t.hide();
//                 },
//                 this
//             );
//         };
//         t.prototype.hide = function () {
//             this.node.destroy();
//         };
//         return __decorate([c], t);
//     })(cc.Component));
// exports.default = l;


const { ccclass, property } = cc._decorator;

/**
 * 游戏窗口类，负责展示游戏相关信息和处理用户交互事件
 */
@ccclass
class GameWindow extends cc.Component {

    /**
     * 显示游戏窗口
     * @param {Object} config 配置对象
     * @param {string} config.gameName 游戏名称
     * @param {cc.SpriteFrame} config.gameIcon 游戏图标
     * @param {cc.SpriteFrame} config.rewardIcon 奖励图标
     * @param {Function} config.onReward 奖励回调函数
     * @param {boolean} config.xianshi_jiangli 是否显示奖励按钮
     */
    show(config) {
        const { gameName, gameIcon, rewardIcon, onReward, xianshi_jiangli } = config;
        
        const windowNode = cc.find("window", this.node);
        const nameLabel1 = cc.find("nameLabel1", windowNode).getComponent(cc.Label);
        const nameLabel2 = cc.find("nameLabel2", windowNode).getComponent(cc.Label);
        const getButton = cc.find("get_btn", windowNode);
        const goButton = cc.find("go_btn", windowNode);
        const iconSprite = cc.find("icon", windowNode).getComponent(cc.Sprite);
        const rewardIconSprite = cc.find("rewardIcon", windowNode).getComponent(cc.Sprite);
        
        windowNode.scale = Math.min(cc.winSize.width / cc.view.getDesignResolutionSize().width, 1);
        
        nameLabel1.string = gameName;
        nameLabel2.string = gameName.length > 4 ? `${gameName.slice(0, 4)}...` : gameName;
        
        getButton.active = xianshi_jiangli;
        goButton.active = !xianshi_jiangli;

        getButton.on(cc.Node.EventType.TOUCH_END, () => {
            console.log("touch get_btn");
            if (onReward) {
                onReward();
            }
            this.hide();
        }, this);

        goButton.on(cc.Node.EventType.TOUCH_END, () => {
            console.log("touch go_btn");
            tt.navigateToScene({
                scene: "sidebar",
                success: () => {
                    console.log("sidebar跳转成功");
                    this.hide();
                },
                fail: () => {
                    console.log("sidebar跳转失败");
                }
            });
        }, this);

        if (gameIcon) {
            iconSprite.spriteFrame = gameIcon;
        }

        if (rewardIcon) {
            rewardIconSprite.spriteFrame = rewardIcon;
        }

        this.node.on(cc.Node.EventType.TOUCH_END, () => {
            this.hide();
        }, this);
    }

    /**
     * 隐藏游戏窗口
     */
    hide() {
        this.node.destroy();
    }
}

export default GameWindow;
